package com.huzz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huzz.entity.SysMenu;
import com.huzz.entity.SysPermission;
import com.huzz.entity.SysRole;
import com.huzz.mapper.SysRoleMapper;
import com.huzz.model.RoleModel;
import com.huzz.service.ISysRoleService;
import com.huzz.util.entityutil.ModelUtil;
import com.huzz.util.body.DataBox;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Name 名称
 * @Description 描述
 * @Author 童年的纸飞机
 * @Since 2023-04-2023/4/27 11:13
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    @Autowired
    private SysRoleMapper roleMapper;

    @Override
    public DataBox<String> createRole(RoleModel roleModel) {
        return DataBox.builder(roleMapper.insert(ModelUtil.transToSimilarEntity(roleModel, SysRole.class)) > 0, "新建成功", "新建失败");
    }

    @Override
    public IPage<SysRole> findRoleListPage(Integer pageNum, Integer pageSize, RoleModel model) {
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(model.getName())) {
            wrapper.like(SysRole::getName, model.getName());
        }
        if (model.getStatus() != null) {
            wrapper.eq(SysRole::getStatus, model.getStatus());
        }

        return roleMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
    }

    @Override
    public DataBox<String> updateRoleById(SysRole role) {
        if (role.getId() == null) {
            return DataBox.failedBuilder("角色id不能为空");
        }
        return DataBox.builder(roleMapper.updateById(role) > 0, "更新成功", "更新失败");
    }

    @Override
    public DataBox<String> removeRoleById(Integer id) {
        if (id == null) {
            return DataBox.failedBuilder("角色id不能为空");
        }
        return DataBox.builder(roleMapper.deleteById(id) > 0, "删除成功", "删除失败");
    }

    @Override
    public List<SysRole> findRoleListByUserId(Integer userId) {
        return roleMapper.findRoleListByUserId(userId);
    }

    @Transactional
    @Override
    public DataBox<String> distributePermission(Integer roleId, List<Integer> permissionIdList) {
        if (permissionIdList.isEmpty()) {
            return DataBox.failedBuilder("permissionIdList至少包含一条记录");
        }
        roleMapper.clearRolePermissionListByRoleId(roleId);

        int num = roleMapper.saveRolePermissionBatch(roleId, permissionIdList);
        if (permissionIdList.size() == num) {
            return DataBox.succeedBuilder("配置成功, 共分配" + num + "个权限");
        }

        return DataBox.succeedBuilder("配置成功, 共分配" + num + "个权限, 分配失败" + (permissionIdList.size() - num) + "个权限");
    }

    @Transactional
    @Override
    public DataBox<String> distributeMenu(Integer roleId, List<Integer> menuIdList) {
        if (menuIdList.isEmpty()) {
            return DataBox.failedBuilder("permissionIdList至少包含一条记录");
        }
        roleMapper.clearRoleMenuListByRoleId(roleId);

        int num = roleMapper.saveRoleMenuIdListBatch(roleId, menuIdList);
        if (menuIdList.size() == num) {
            return DataBox.succeedBuilder("配置成功, 共分配" + num + "个菜单");
        }

        return DataBox.succeedBuilder("配置成功, 共分配" + num + "个菜单, 分配失败" + (menuIdList.size() - num) + "个菜单");
    }

    @Override
    public List<SysPermission> findPermissionListByRoleId(Integer roleId) {
        return roleMapper.findPermissionListByRoleId(roleId);
    }

    @Override
    public List<SysMenu> findMenuListByRoleId(Integer roleId) {
        return roleMapper.findMenuListByRoleId(roleId);
    }

    @Override
    public Integer[] findPermissionIdListBeSelected(Integer roleId) {
        List<SysPermission> permissionList = findPermissionListByRoleId(roleId);
        if (permissionList.isEmpty()) {
            return new Integer[0];
        }

        return permissionList.stream().map(m -> m.getId()).collect(Collectors.toList()).toArray(new Integer[permissionList.size()]);
    }

    @Override
    public Integer[] findMenuIdListBeSelected(Integer roleId) {
        List<SysMenu> menuList = findMenuListByRoleId(roleId);
        if (menuList.isEmpty()) {
            return new Integer[0];
        }

        return menuList.stream().map(m -> m.getId()).collect(Collectors.toList()).toArray(new Integer[menuList.size()]);
    }
}
